package com.maisha.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {

	/** 从cookie 用户登录的信息，
	 * @param request
	 * @return
	 */
	public static String getCookie(HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		String userAndPass = "";
		for(Cookie c : cookies){
			if(CMSConstant.COOKIE_AUDITOR_KEY.equals(c.getName())){
				userAndPass = c.getValue();
			}
		}
		return userAndPass;
	}
	
	/** 设在cookie
	 * @param request
	 * @param userName
	 * @param password
	 */
	public static void setCookie(HttpServletResponse response,String content,int maxAge){
		Cookie c = new Cookie(CMSConstant.COOKIE_AUDITOR_KEY, content);
		c.setMaxAge(maxAge);
		c.setPath("/");
		response.addCookie(c);
	}
	
	/** 移除cookie内容 即： 设置cookie 的value 为null 并且立即过期
	 * @param response
	 * @param key
	 */
	public static void deleteCookie(HttpServletResponse response,String key){
		Cookie cookie = new Cookie(key, null); 
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie); 
	}
	
}
